--
-- Author: mm
-- Date: 2018-08-14 15:28:33
--
-- NewerbattleguideRoleView
-- 新手战选角UI

local Armature = app:getClass("Armature")
local NewerbattleguideRoleRender = import(".renders.NewerbattleguideRoleRender")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.newerBattleGuideRoleUI

local roleNum = 4 -- 角色个数
local selectedRoleNodePos -- 选中的角色节点初始位置

function ClassRef:init()
	self._aniEnd = true
	self._ui.tlt:setVisible( false )
	self._ui.btn_right:setVisible( false )
	display.uiAddClick( self._ui.mask, function() -- 返回按钮
		self:_playUnselectedAni()
	end)
	display.uiAddClick( self._ui.btn_right, function() -- 确认按钮
		AM.playSoundById(2402)
		self:_playSelectedSureAni()
	end)

	-- 初始化备选的4个角色
	self._roleList = {}
	for i = 1, roleNum do
		local temp = NewerbattleguideRoleRender.createWithData( self._data[i], self._ui[ "role_" .. i ] )
		temp:getTimeline():gotoFrameAndPause( 0 )
		temp:setParticlePlay( false )
		temp:onBtnClick( function()
			AM.playSoundById(3692)
			self:_playSelectedAni( i )
		end )
		self._roleList[i] = temp
	end
	self._selectedRoleIdx = 1
	-- 选中的角色节点
	self._selectedRoleNode = NewerbattleguideRoleRender.create( self._ui.role )
	self._selectedRoleNode:setCanBeTouch( false )
	selectedRoleNodePos = cc.p( self._selectedRoleNode:getPosition() )
	self._selectedRoleNode:setVisible( false )
	self._selectedRoleNode:getTimeline():gotoFrameAndPause( 0 )
	self._selectedRoleNode:setParticlePlay( false )

	self:_refreshSelectedRoleInfo( false )
end

-- 播放选中节点动画
function ClassRef:_playSelectedAni( idx )
	if self._flagSelectedSure then return end
	if not self._aniEnd then return end
	print( " -------------->>>> 选择" )

	self._aniEnd = false
	self._selectedRoleIdx = idx
	self._selectedRoleNode:setData( self._data[idx] )
	self._ui.tlt:setString( L("lua_code_text_204") .. idx )
	self:_refreshSelectedRoleInfo( true )

	-- 设置动画起始参数
	self._selectedRoleNode:getTimeline():gotoFrameAndPause( 0 )
	self._selectedRoleNode:setScale( self._roleList[idx]:getScale() )
	self._selectedRoleNode:setPosition( self._roleList[idx]:getPosition() )
	self._selectedRoleNode:setVisible( true )
	-------------------------------->>>>>>>>>>>>>>>>>>>>> 选中卡牌动画
	local Action_1 = cc.ScaleTo:create( 0.333, 1 )
	local Action_2 = cc.MoveTo:create( 0.333, selectedRoleNodePos )
	-- 卡牌翻转动画
	-- 持续时间、半径初始值、半径增量、仰角初始值、仰角增量、离x轴的偏移角、离x轴的偏移角的增量
	local Action_3 = cc.OrbitCamera:create( 0.333, 10, 0, 0, -360, 0, 0 )
	local Actions_1 = cc.Spawn:create( Action_1, Action_2, Action_3 )

	local Actions_2 = cc.CallFunc:create(function()
		-- 动画完成回调
		self._aniEnd = true
		self._selectedRoleNode:getTimeline():play( "float", true )
		self._ui.btn_right:setVisible( true )
	end)
	local actions = cc.Sequence:create( Actions_1, Actions_2 )
	self._selectedRoleNode:runAction( actions )
end

-- 取消选择角色动画
function ClassRef:_playUnselectedAni()
	if self._flagSelectedSure then return end
	if not self._aniEnd then return end
	print( " -------------->>>> 返回" )
	self._ui.btn_right:setVisible( false )
	self._aniEnd = false
	local scaleTo = self._roleList[self._selectedRoleIdx]:getScale()
	local posTo = cc.p( self._roleList[self._selectedRoleIdx]:getPosition() )
	-------------------------------->>>>>>>>>>>>>>>>>>>>> 取消选中卡牌动画
	local Action_1 = cc.ScaleTo:create( 0.333, scaleTo )
	local Action_2 = cc.MoveTo:create( 0.333, posTo )
	-- 卡牌翻转动画
	-- 持续时间、半径初始值、半径增量、仰角初始值、仰角增量、离x轴的偏移角、离x轴的偏移角的增量
	local Action_3 = cc.OrbitCamera:create( 0.333, 10, 0, 0, -360, 0, 0 )
	local Actions_1 = cc.Spawn:create( Action_1, Action_2, Action_3 )
	local Actions_2 = cc.CallFunc:create(function()
		-- 动画完成回调
		self._selectedRoleIdx = 1
		self._selectedRoleNode:setData()
		self:_refreshSelectedRoleInfo( false )
		self._aniEnd = true
	end)
	local actions = cc.Sequence:create( Actions_1, Actions_2 )
	self._selectedRoleNode:runAction( actions )
end

-- 播放角色确认动画
function ClassRef:_playSelectedSureAni()
	-- 爆炸特效
	print( " -------------->>>> 确认" )
	self._flagSelectedSure = true
	local function playExplodeEffect()
		local params = {
			armatureName = "getride",
			path = Res.MountNewEfcPath
		}
		local armature = Armature.create(params)
		self:addChild(armature, 2)
		armature:setPosition(display.cx, display.cy)
		AM.playSoundById(2392)
		armature:play("getride", 1, function(sender)
			sender:removeFromParent()
		end)
		armature:onFrameEvent(function(sender, name)
			if name == "all" then
				executeFunc( self._onOperate, "selectedRoleEnd", self._data[ self._selectedRoleIdx ] )
				executeFunc( self._onCloseView )
			end
		end)
	end

	executeFunc( self._onOperate, "selectedRoleStart", self._selectedRoleIdx )

	self._selectedRoleNode:setParticlePlay( true )
	self._selectedRoleNode:getTimeline():play( "light", false )
--	self._selectedRoleNode:getTimeline():setLastFrameCallFunc(function()
--		playExplodeEffect()
--	end)
	self:performWithDelay(playExplodeEffect, 0.4)
	self._ui.btn_right:setVisible(false)
	self._ui.mask:runAction(cc.FadeIn:create(1))
end

-- 刷新选中角色的详情UI
function ClassRef:_refreshSelectedRoleInfo( selected )
	selected = selected or false
	if selected then -- 选中了具体角色?
		self._ui.mask:setVisible( true )
		self._selectedRoleNode:setVisible( true )
--		self._ui.tlt:setVisible( true )
	else
		self._ui.mask:setVisible( false )
		self._selectedRoleNode:setVisible( false )
--		self._ui.tlt:setVisible( false )
	end
end

return ClassRef
